﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySearch;
using Kpmg.CompanyInfo;

public partial class User_CreateTicketFromProfiles : System.Web.UI.Page
{
    string _iid = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        _iid = Utilities.getLoginIID();

        if (!IsPostBack)
        {
            init();
        }
    }

    public void init()
    {
        Staff staff = StaffDAO.GetObjectByUserId(_iid);
        if (staff == null)
            return;

        //prepare client profiles
        List<Chargeable> lstChargeable = new List<Chargeable>();
        IList<Profile> lstProfile = ProfileOM.ListProfile(staff.StaffNo);
        foreach (Profile profile in lstProfile)
        {
            lstChargeable.AddRange(profile.Chargeables);
        }
        lstChargeable.AddRange(ChargeableOM.ListMyChargeable(staff.StaffNo));
        lstChargeable = lstChargeable.OrderBy(x => x.ProfileCode).OrderBy(y => y.ChargeableCode).Distinct().ToList();
        foreach (Profile profile in lstProfile)
        {
            Chargeable c = new Chargeable();
            c.ChargeableType = ChargeableType.NA;
            c.ProfileCode = profile.ProfileCode;
            c.Description = profile.Description;
            lstChargeable.Add(c);
        }
        this.rptChargeable.DataSource = lstChargeable;
        this.rptChargeable.DataBind();

        // prepare no-client profiles
        IList<Profile> lstNoClientProfile = new List<Profile>();
        Profile pro = new Profile();
        pro.ProfileCode = "0";
        pro.ProfileType = ProfileType.Other;
        pro.Description = "General";
        lstNoClientProfile.Add(pro);

        IList<Profile> lstServiceTeamProfile = ProfileOM.ListServiceTeamProfile();
        foreach (Profile obj in lstServiceTeamProfile)
        {
            TeamRole? role = UserInfoOM.GetTeamRole(staff.StaffNo, obj.ProfileCode);
            if (role != null)
                lstNoClientProfile.Add(obj);
        }
        this.rptNoClientProfile.DataSource = lstNoClientProfile;
        this.rptNoClientProfile.DataBind();

        FillDDL();
        this.ddlCategory.Attributes.Add("onchange", "selectIndexChanged(this,'Category');");
        this.ddlServiceType.Attributes.Add("onchange", "selectIndexChanged(this,'ServiceType');");
    }

    protected void btnCreateTicket_Click(object sender, EventArgs e)
    {
        RemotePost _remotePost = new RemotePost();

        if (this.txtProfileType.Text.Equals("Client"))
        {
            string profileCode = this.txtProfileCode.Text;
            string chargeableCode = this.txtChargeableCode.Text;
            string serviceTypeCode = this.txtServiceTypeCode.Text;

            _remotePost.Add("profileCode", profileCode);
            _remotePost.Add("chargeablecode", chargeableCode);
            _remotePost.Add("serviceTypeCode", serviceTypeCode);
        }
        else if (this.txtProfileType.Text.Equals("NonClient"))
        {
            string profileCode = this.txtProfileCode.Text;
            string serviceTypeCode = this.txtServiceTypeCode.Text;
            _remotePost.Add("profileCode", profileCode);
            _remotePost.Add("serviceTypeCode", serviceTypeCode);
        }

        _remotePost.Url = ResolveUrl("~/user/CreateTicket.aspx");
        _remotePost.Post();
    }

    #region repeater

    protected void rptChargeable_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Chargeable chargeable = (Chargeable)e.Item.DataItem;
            Profile profile = ProfileOM.GetProfile(chargeable.ProfileCode);

            if (chargeable.ChargeableType == ChargeableType.Engagement)
            {
                ((Label)e.Item.FindControl("lblType")).Text = chargeable.ChargeableType.ToString();
                ((Label)e.Item.FindControl("lblProfile")).Text = profile.ProfileCode + "-" + profile.Description;
                ((Label)e.Item.FindControl("lblChargeableToCode")).Text = chargeable.ChargeableCode;
                ((Label)e.Item.FindControl("lblChargeableDesc")).Text = chargeable.Description;
                ((Label)e.Item.FindControl("lblStatus")).Text = "Engagement status:" + chargeable.Status;

                ((Label)e.Item.FindControl("lblTeamInherit")).Text = "Team inherited from profile";
                if (chargeable.IsMemberInherited == false)
                {
                    ((Label)e.Item.FindControl("lblTeamInherit")).Text = "Team manually managed";
                    ((Image)e.Item.FindControl("Image1")).Visible = true;
                }

                Button BtnCreateTicket = (Button)e.Item.FindControl("BtnCreateTicket");
                BtnCreateTicket.Attributes.Add("ProfileCode", profile.ProfileCode);
                BtnCreateTicket.Attributes.Add("ChargeableCode", chargeable.ChargeableCode);
                BtnCreateTicket.Text = "Create new ticket";
            }
            else
            {
                ((Label)e.Item.FindControl("lblType")).Text = "Default";
                ((Label)e.Item.FindControl("lblProfile")).Text = profile.ProfileCode + "-" + profile.Description;
                ((Label)e.Item.FindControl("lblChargeableDesc")).Text = "Chargeable TBD";

                Button BtnCreateTicket = (Button)e.Item.FindControl("BtnCreateTicket");
                BtnCreateTicket.Attributes.Add("ProfileCode", profile.ProfileCode);
                BtnCreateTicket.Attributes.Add("ChargeableCode", chargeable.ChargeableCode);
                BtnCreateTicket.Text = "Create new ticket";
            }
        }

        if (e.Item.ItemType == ListItemType.Footer)
        {
            IList<Chargeable> source = this.rptChargeable.DataSource as IList<Chargeable>;
            if (source.Count == 0)
                ((Panel)e.Item.FindControl("panelFooter")).Visible = true;
        }
    }

    protected void rptNoClientProfile_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Profile obj = e.Item.DataItem as Profile;

            if (obj != null)
            {
                string profileType = "Service team";
                if (obj.ProfileType == ProfileType.Other)
                    profileType = "Internal";

                ((Label)e.Item.FindControl("lblType")).Text = profileType;
                ((Label)e.Item.FindControl("lblProfile")).Text = obj.Description;

                Button BtnCreateTicket = (Button)e.Item.FindControl("btnCreateNonClientTicket");
                if (obj.ProfileType == ProfileType.Other)
                    BtnCreateTicket.Attributes.Add("ProfileCode", "0");
                else
                    BtnCreateTicket.Attributes.Add("ProfileCode", obj.ProfileCode);

                BtnCreateTicket.Text = "Create new ticket";
            }
        }
    }
    #endregion

    #region private

    private void FillDDL()
    {
        //Client
        IList<ServiceCategory> lstClientCategory = ServiceTemplateOM.ListServiceCategories();
        this.ddlClientCategory.DataSource = lstClientCategory;
        this.ddlClientCategory.DataTextField = "Description";
        this.ddlClientCategory.DataValueField = "CategoryCode";
        this.ddlClientCategory.DataBind();

        List<ServiceType> lstClientServiceType = new List<ServiceType>();
        foreach (ServiceCategory sc in lstClientCategory)
        {
            lstClientServiceType.AddRange(ServiceTemplateOM.ListServiceTypes(sc.CategoryCode));
        }
        this.ddlClientServiceType.DataSource = lstClientServiceType;
        this.ddlClientServiceType.DataTextField = "Description";
        this.ddlClientServiceType.DataValueField = "TypeCode";
        this.ddlClientServiceType.DataBind();

        //Non Client
        IList<ServiceCategory> lstNonClientCategory = ServiceTemplateOM.ListServiceCategories();
        this.ddlNonClientCategory.DataSource = lstNonClientCategory;
        this.ddlNonClientCategory.DataTextField = "Description";
        this.ddlNonClientCategory.DataValueField = "CategoryCode";
        this.ddlNonClientCategory.DataBind();

        List<ServiceType> lstNonClientServiceType = new List<ServiceType>();
        foreach (ServiceCategory sc in lstNonClientCategory)
        {
            lstNonClientServiceType.AddRange(ServiceTemplateOM.ListServiceTypes(sc.CategoryCode));
        }
        this.ddlNonClientServiceType.DataSource = lstNonClientServiceType;
        this.ddlNonClientServiceType.DataTextField = "Description";
        this.ddlNonClientServiceType.DataValueField = "TypeCode";
        this.ddlNonClientServiceType.DataBind();

    }

    #endregion
}